package com.collectplus.express.parcel;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.DialogMgr;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.ParcelDetailBeanNew;
import com.collectplus.express.model.ParcelRouteBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

import droid.frame.utils.GlideUtil;

/**
 * Created by lidandan on 2018/3/9.
 * 我的包裹-----在途中的包裹详情
 */
public class NewParcelDetailActivity extends BaseActivity2 implements View.OnClickListener {

    private ParcelDetailBeanNew bean;
    private LinearLayout routeLayout;
    private View mExpressInfoLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        String parcelId = getIntent().getStringExtra("parcelId");
        bean = (ParcelDetailBeanNew) getIntent().getSerializableExtra("item");
        super.onCreate(savedInstanceState);
        if (bean == null) {
            if (isNotEmpty(parcelId)) {
                AppHttp.getInstance().getParcelDetailNew(parcelId, 0);
            } else {
                showToast("数据有误");
            }
        } else {
            updateDataToView(bean);
        }
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.new_parcel_detail_activity);
        super.findViewById();
        getAppTitle().setCommonTitle("包裹详情");
        routeLayout = (LinearLayout) findViewById(R.id.parcel_route_parent);
        mExpressInfoLayout = findViewById(R.id.express_info_ll);
        findViewById(R.id.confirm_signed_bt).setOnClickListener(this);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //获取包裹详情
            case MsgID.parcel_detail:
                cancelLoadingDialog();
                final AppResult<ParcelDetailBeanNew> result = JSON.parser(msg.obj, ParcelDetailBeanNew.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (result.isSuccess()) {
                            bean = result.getResult();
                            if (bean != null) {
                                updateDataToView(bean);
                            }
                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });
                return false;
            //在途中确认包裹已取走
            case MsgID.confirm_collecting_parcel_detail:
                cancelLoadingDialog();
                AppResult<?> appResult = JSON.parser(msg.obj);
                if (appResult.isSuccess()) {
                    finish();
                } else {
                    showToast(appResult.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }

    }

    /**
     * 格式化时间
     *
     * @param time
     * @return
     */
    private String getTime(long time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return formatter.format(new Date(time));
    }

    private void updateDataToView(ParcelDetailBeanNew bean) {
        TextView sendNameTV = (TextView) findViewById(R.id.parcel_send_name_tv);
        TextView sendAddressTV = (TextView) findViewById(R.id.parcel_send_address_tv);
        TextView receiverNameTV = (TextView) findViewById(R.id.parcel_receiver_name_tv);
        TextView receiveAddressTv = (TextView) findViewById(R.id.parcel_receiver_address_tv);
        ImageView parcelMessageImage = (ImageView) findViewById(R.id.parcel_message_image);
        parcelMessageImage.setOnClickListener(this);
        sendNameTV.setText(bean.getSenderName());
        receiverNameTV.setText(bean.getReceiverName());
        //同城包裹详情
        if (bean.isCityWide()) {
            mExpressInfoLayout.setVisibility(View.GONE);
            findViewById(R.id.samecity_left_view).setVisibility(View.VISIBLE);
            //底部操作提示
            findViewById(R.id.bottom_view_layout).setVisibility(View.GONE);
            sendAddressTV.setText(bean.getUserBuildName());
            receiveAddressTv.setText(bean.getReceiverBuildName());
        } else {//代收包裹详情
            mExpressInfoLayout.setVisibility(View.VISIBLE);
            findViewById(R.id.samecity_left_view).setVisibility(View.GONE);
            findViewById(R.id.bottom_view_layout).setVisibility(View.VISIBLE);
            if (isNotEmpty(bean.getExpressNumber())) {
                ((TextView) findViewById(R.id.express_info_tv)).setText(bean.getExpressCompanyName() + "：" + bean.getExpressNumber());
            } else {
                ((TextView) findViewById(R.id.express_info_tv)).setText(bean.getExpressCompanyName() + "：暂无");
            }
            sendAddressTV.setText(bean.getUserCity());
            receiveAddressTv.setText(bean.getReceiverCity());
        }
        GlideUtil.loadImage(getContext(), bean.getParcelImgUrl(), 0, parcelMessageImage);
        routeInformationShow(routeLayout, bean);
    }

    /**
     * 路由信息展示
     */
    private void routeInformationShow(LinearLayout routeLayout, ParcelDetailBeanNew bean) {
        ArrayList<ParcelRouteBean> routes = bean.getRoutes();
        if (null != routes && routes.size() > 0) {
            // 添加item
            for (int position = 0; position < routes.size(); position++) {
                ParcelRouteBean route = routes.get(position);
                String updateTime = getTime(route.getUpdateTime());
                View item = getLayoutInflater().inflate(R.layout.parcel_status_item, null);
                TextView time = (TextView) item.findViewById(R.id.parcel_status_time);
                TextView status = (TextView) item.findViewById(R.id.parcel_status_content);
                time.setText(updateTime);
                status.setText(route.getDescription());
                poitView(item, routes, position);
                routeLayout.addView(item);
            }
        }
    }


    private void poitView(View item, ArrayList<ParcelRouteBean> routes, int position) {
        ImageView poitView = (ImageView) item.findViewById(R.id.parcel_status_point);
        // 设置圆点颜色 调用的老版代码
        if (routes.size() == 1) {
            poitView.setImageResource(R.drawable.parcel_detail_dot2);
        } else {
            if (position == 0) {
                poitView.setImageResource(R.drawable.parcel_detail_dot1);
            } else if (position == 1) {
                poitView.setImageResource(R.drawable.parcel_detail_dot2);
            } else {
                // 中间图片随机展示
                int randomInt = new Random().nextInt(12) % 10 + 3;
                int resId = getContext().getResources().getIdentifier("parcel_detail_dot" + randomInt, "drawable", getContext().getPackageName());
                poitView.setImageResource(resId);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //确认已签收按钮
            case R.id.confirm_signed_bt:
                if (bean != null && isNotEmpty(bean.getParcelId())) {
                    showLoadingDialog(null);
                    AppHttp.getInstance().confirmCollectingParcelDetail(bean.getParcelId());
                }
                break;
            //点击查看包裹图片
            case R.id.parcel_message_image:
                if (bean != null && isNotEmpty(bean.getParcelImgUrl())) {
                    DialogMgr.showLargeStationPhoto(getContext(), bean.getParcelImgUrl());
                }
                break;
        }
    }
}
